/*
 * Copyright 2009 Jinbo Lin. All rights reserved.
 * 
 * cn.edu.pku.dao.impl HumanResourcesManagementSystem
 *
 * Created on 2009-12-20
 */
package cn.edu.pku.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.pku.dao.TitlemgtDao;
import cn.edu.pku.model.Titlemgt;

/**
 * @author Jinbo Lin
 * 
 *  
 */
public class TitlemgtDaoHibernate extends HibernateDaoSupport implements
		TitlemgtDao {

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		if (null != this.get(id)) {
			this.getHibernateTemplate().delete(
					this.getHibernateTemplate().get(Titlemgt.class, id));
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#delete(cn.edu.pku.model.Titlemgt)
	 */
	@Override
	public void delete(Titlemgt titlemgt) {
		if (null != this.get(titlemgt.getTitleId())) {
			this.getHibernateTemplate().delete(titlemgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Titlemgt> findAll() {
		String hql = "from Titlemgt";
		return this.getHibernateTemplate().find(hql);
	}
	
	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#get(int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Titlemgt get(int id) {
		String hql = "from Titlemgt titlemgt where titlemgt.titleId=?";
		List<Titlemgt> titlemgts = this.getHibernateTemplate().find(
				hql, new Integer(id));
		Titlemgt titlemgt = (null != titlemgts && 0 != titlemgts.size()) ? titlemgts.get(0) : null;
		return titlemgt;
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#save(cn.edu.pku.model.Titlemgt)
	 */
	@Override
	public Serializable save(Titlemgt titlemgt) {
		if(null == this.get(titlemgt.getTitleId())) {
			this.getHibernateTemplate().save(titlemgt);
		} else {
			titlemgt = null;
		}
		return titlemgt;
		
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#update(cn.edu.pku.model.Titlemgt)
	 */
	@Override
	public void update(Titlemgt titlemgt) {
		if (null != this.get(titlemgt.getTitleId())) {
//			this.getHibernateTemplate().saveOrUpdate(title);
			this.getHibernateTemplate().update(titlemgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.TitlemgtDao#tDelete(int)
	 */
	@Override
	public void tDelete(int id) {
		Titlemgt titlemgt = this.get(id);
		titlemgt.setRemark("Removed");
		this.save(titlemgt);
	}
}
